package com.wry.wry_test.feign.config;

/**
 * @Author: wangrongyi
 * @Date: 2024/7/24 17:36
 * @Description: 加权轮询算法
 */
public class WeightedRoundRobin implements LoadBalanceService {
    private final LoadBalanceProperties properties;

    public WeightedRoundRobin(LoadBalanceProperties loadBalanceProperties) {
        this.properties = loadBalanceProperties;
    }

    private int weightCount = 0;
    private int index = 0;

    @Override
    public String getServerAddress() {
        int weight = properties.getWeight().get(properties.getAddress().get(index));
        if (weightCount == weight) {
            weightCount = 0;
            index = (index + 1) % properties.getAddress().size();
        }
        weightCount++;
        return properties.getAddress().get(index);

    }
}
